package com.sinosoft.app.packmarket.biz.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sinosoft.app.packmarket.biz.PackOrderBiz;
import com.sinosoft.app.packmarket.dao.PackEquityDao;
import com.sinosoft.app.packmarket.dao.PackOrderDao;
import com.sinosoft.app.packmarket.entity.PackEquity;
import com.sinosoft.app.packmarket.entity.PackOrder;
import com.sinosoft.system.bank.dao.BankPersonalDao;
import com.sinosoft.system.bank.dao.BankTaxDao;
import com.sinosoft.system.bank.dao.BankTradeDao;
import com.sinosoft.system.bank.entity.BankPersonal;
import com.sinosoft.system.bank.entity.BankTrade;
import com.sinosoft.system.bank.util.BankUtil;
import com.sinosoft.system.util.date.DateUtil;
import com.sinosoft.system.util.page.Pagenation;

@Service
@Transactional
public class PackOrderBizImp implements PackOrderBiz{

	private static Logger log = Logger.getLogger(PackOrderBizImp.class);
	
	@Autowired
	private PackOrderDao packOrderDao;
	
	@Autowired
	private BankPersonalDao bankPersonalDao;
	
	@Autowired
	private BankTradeDao bankTradeDao;
	
	@Autowired
	private BankTaxDao bankTaxDao;
	
	@Autowired
	private	PackEquityDao packEquityDao;
	
	/**
	 * 0、先判断用户金额是否够支付
	 * 1、包裹流水入库
	 * 2、购买者扣款
	 * 3、发布者入款
	 * 4、系统抽成
	 * 5、生成交易流水单
	 * 
	 * @param packOrder
	 * @return 0交易失败，1交易成功
	 */
	@Override
	public int insertPackOrder(PackOrder packOrder) {
		log.info("新增PackOrder数据"+packOrder.toString());
		try{
			List<String> queryBuy = new ArrayList<String>();
			queryBuy.add("sys_user_id");
			List<Object> valueBuy = new ArrayList<Object>();
			valueBuy.add(packOrder.getSysUserId());
			BankPersonal personBuy = bankPersonalDao.selectBankPersonal(queryBuy, valueBuy);
			if(personBuy==null|| BankUtil.compare(personBuy.getAssets(), packOrder.getMoney())<=0 ){//0、先判断用户金额是否够支付
				return 0;
			}else{
				packOrderDao.insertPackOrder(packOrder);//1、包裹流水入库
				
				personBuy.setAssets(BankUtil.sub(personBuy.getAssets(), packOrder.getMoney()));
				bankPersonalDao.updateBankPersonal(personBuy);//2、购买者扣款
				BankTrade buyTrade = new BankTrade();
				buyTrade.setSysUserId(personBuy.getSysUserId());
				buyTrade.setTradename(personBuy.getPersoname());
				buyTrade.setTradeint(packOrder.getMoney());
				buyTrade.setTradetime(DateUtil.getDate(new Date()));
				buyTrade.setTrade("0");
				buyTrade.setTradeinfo("购买股票包裹");
				bankTradeDao.insertBankTrade(buyTrade);//生成交易流水
				
				PackEquity packEquity = packEquityDao.selectPackEquity(packOrder.getPackEquityId());
				List<String> queryRls = new ArrayList<String>();
				queryRls.add("sys_user_id");
				List<Object> valueRls = new ArrayList<Object>();
				valueRls.add(packEquity.getSysUserId());
				BankPersonal bankReleaser = bankPersonalDao.selectBankPersonal(queryRls, valueRls);
				List<String> queryTax = new ArrayList<String>();
				queryTax.add("module");
				List<Object> valueTax = new ArrayList<Object>();
				valueTax.add("pack");
				String tax = bankTaxDao.selectBankTax(queryTax, valueTax).getTax(); //获取税率
				bankReleaser.setAssets(BankUtil.add(bankReleaser.getAssets(), BankUtil.mul(packOrder.getMoney(), BankUtil.sub("1", tax))));
				bankPersonalDao.updateBankPersonal(bankReleaser);//3、发布者入款
				BankTrade releaserTrade = new BankTrade();
				releaserTrade.setSysUserId(bankReleaser.getSysUserId());
				releaserTrade.setTradename(bankReleaser.getPersoname());
				releaserTrade.setTradeint(BankUtil.mul(packOrder.getMoney(), BankUtil.sub("1", tax)));
				releaserTrade.setTradetime(DateUtil.getDate(new Date()));
				releaserTrade.setTrade("1");
				releaserTrade.setTradeinfo("售出股票包裹");
				bankTradeDao.insertBankTrade(releaserTrade);//生成交易流水
				
				BankPersonal bankSys = bankPersonalDao.selectBankPersonal(1);
				bankSys.setAssets(BankUtil.add(bankReleaser.getAssets(), BankUtil.mul(packOrder.getMoney(), tax)));//4、系统抽成
				BankTrade sysTrade = new BankTrade();
				sysTrade.setSysUserId(bankSys.getSysUserId());
				sysTrade.setTradename(bankSys.getPersoname());
				sysTrade.setTradeint(BankUtil.mul(packOrder.getMoney(), tax));
				sysTrade.setTradetime(DateUtil.getDate(new Date()));
				sysTrade.setTrade("1");
				sysTrade.setTradeinfo("股票包裹系统抽成");
				bankTradeDao.insertBankTrade(sysTrade);//生成交易流水
				
				return 1;
			}
		}catch(Exception e){
			log.error(e.getMessage());
		}				
		return 0;		
	}

	@Override
	public int updatePackOrder(PackOrder packOrder) {
		log.info("更新PackOrder数据"+packOrder.toString());
		return packOrderDao.updatePackOrder(packOrder);
	}

	@Override
	public int deletePackOrder(int id) {
		log.info("删除PackOrder数据 id="+id);
		return packOrderDao.deletePackOrder(id);
	}

	@Override
	public PackOrder selectPackOrder(int id) {
		log.info("根据ID查询PackOrder数据 id="+id);
		return packOrderDao.selectPackOrder(id);
	}

	@Override
	public PackOrder selectPackOrder(List<String> queryKeys,List<Object> queryValues) {
		log.info("查询PackOrder数据 ");
		return packOrderDao.selectPackOrder(queryKeys, queryValues);
	}

	@Override
	public int selectCountPackOrder() {
		log.info("查询PackOrder数据 总数量");
		return packOrderDao.selectCountPackOrder();
	}

	@Override
	public int selectCountPackOrder(List<String> queryKeys,List<Object> queryValues) {
		log.info("查询PackOrder数据 总数量"+queryKeys.toString());
		return packOrderDao.selectCountPackOrder(queryKeys, queryValues);
	}

	@Override
	public List<PackOrder> selectLimitPackOrder(int start, int size) {
		log.info("查询PackOrder数据 limit数量:start="+start+",size="+size);
		return packOrderDao.selectLimitPackOrder(start, size);
	}

	@Override
	public List<PackOrder> selectLimitPackOrder(List<String> queryKeys,List<Object> queryValues,int start, int size) {
		log.info("条件查询PackOrder数据数量");
		return packOrderDao.selectLimitPackOrder(queryKeys, queryValues, start,  size);
	}

	@Override
	public List<PackOrder> selectAllPackOrder() {
		log.info("查询所有PackOrder数据");
		return packOrderDao.selectAllPackOrder();
	}

	@Override
	public List<PackOrder> selectAllPackOrder(List<String> queryKeys,List<Object> queryValues) {
		log.info("条件查询所有PackOrder数据");
		return packOrderDao.selectAllPackOrder(queryKeys, queryValues);
	}

	@Override
	public Pagenation selectPagePackOrder(Pagenation page) {
		log.info("分页查询所有PackOrder数据");
		int start =	page.getStartRow();
		int size = page.getPageSize();
		List<PackOrder> rows = packOrderDao.selectLimitPackOrder(page.getQueryKeys(), page.getQueryValues(),start,size);
		int total = packOrderDao.selectCountPackOrder(page.getQueryKeys(), page.getQueryValues());
		page.setTotal(total);
		page.setRows(rows);
		return page;
	}

	public PackOrderDao getPackOrderDao() {
		return packOrderDao;
	}

	public void setPackOrderDao(PackOrderDao packOrderDao) {
		this.packOrderDao = packOrderDao;
	}

	public BankPersonalDao getBankPersonalDao() {
		return bankPersonalDao;
	}

	public void setBankPersonalDao(BankPersonalDao bankPersonalDao) {
		this.bankPersonalDao = bankPersonalDao;
	}

	public BankTradeDao getBankTradeDao() {
		return bankTradeDao;
	}

	public void setBankTradeDao(BankTradeDao bankTradeDao) {
		this.bankTradeDao = bankTradeDao;
	}

	public BankTaxDao getBankTaxDao() {
		return bankTaxDao;
	}

	public void setBankTaxDao(BankTaxDao bankTaxDao) {
		this.bankTaxDao = bankTaxDao;
	}

	public PackEquityDao getPackEquityDao() {
		return packEquityDao;
	}

	public void setPackEquityDao(PackEquityDao packEquityDao) {
		this.packEquityDao = packEquityDao;
	}

	
}
